load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load(":def.bzl", "std_package_list")

# gazelle:exclude testdata

# TODO(jayconrod): test that the checked-in static file matches the generated
# file. The generated code is checked in so that Gazelle can still be built
# with "go get".
std_package_list(
    name = "std_package_list",
    out = "std_package_list.go",
)

# gazelle:exclude gen_platform_info.go
run_binary(
    name = "gen_platform_info",
    outs = ["gen_platform_info.go"],
    args = ["$(location :gen_platform_info.go)"],
    tool = "//language/go/platform_info_generator",
)

diff_test(
    name = "platform_info_diff_test",
    file1 = ":gen_platform_info.go",
    file2 = "platform_info.go",
)

go_library(
    name = "go",
    srcs = [
        "build_constraints.go",
        "config.go",
        "constants.go",
        "embed.go",
        "fileinfo.go",
        "fix.go",
        "generate.go",
        "kinds.go",
        "lang.go",
        "modules.go",
        "package.go",
        "platform_info.go",
        "resolve.go",
        "std_package_list.go",
        "stdlib_links.go",
        "update.go",
        "utils.go",
        "work.go",
    ],
    importpath = "github.com/bazelbuild/bazel-gazelle/language/go",
    visibility = ["//visibility:public"],
    deps = [
        "//config",
        "//flag",
        "//internal/module",
        "//internal/version",
        "//label",
        "//language",
        "//language/proto",
        "//pathtools",
        "//repo",
        "//resolve",
        "//rule",
        "@com_github_bazelbuild_buildtools//build",
        "@org_golang_x_mod//modfile",
        "@org_golang_x_mod//module",
        "@org_golang_x_sync//errgroup",
    ],
)

go_test(
    name = "go_test",
    srcs = [
        "build_constraints_test.go",
        "config_test.go",
        "fileinfo_go_test.go",
        "fileinfo_test.go",
        "fix_test.go",
        "generate_test.go",
        "resolve_test.go",
        "stubs_test.go",
        "update_import_test.go",
    ],
    data = glob(
        ["testdata/**"],
        # Empty when distributed.
        allow_empty = True,
    ),
    embed = [":go"],
    deps = [
        "//config",
        "//label",
        "//language",
        "//language/proto",
        "//merger",
        "//pathtools",
        "//repo",
        "//resolve",
        "//rule",
        "//testtools",
        "//walk",
        "@com_github_bazelbuild_buildtools//build",
        "@com_github_google_go_cmp//cmp",
        "@io_bazel_rules_go//go/tools/bazel",
        "@org_golang_x_tools_go_vcs//:vcs",
    ],
)

filegroup(
    name = "all_files",
    testonly = True,
    srcs = [
        "BUILD.bazel",
        "build_constraints.go",
        "build_constraints_test.go",
        "config.go",
        "config_test.go",
        "constants.go",
        "def.bzl",
        "embed.go",
        "fileinfo.go",
        "fileinfo_go_test.go",
        "fileinfo_test.go",
        "fix.go",
        "fix_test.go",
        "generate.go",
        "generate_test.go",
        "kinds.go",
        "lang.go",
        "modules.go",
        "package.go",
        "platform_info.go",
        "reference.md",
        "resolve.go",
        "resolve_test.go",
        "std_package_list.go",
        "stdlib_links.go",
        "stubs_test.go",
        "update.go",
        "update_import_test.go",
        "utils.go",
        "work.go",
        "//language/go/gen_std_package_list:all_files",
        "//language/go/platform_info_generator:all_files",
    ],
    visibility = ["//visibility:public"],
)

alias(
    name = "go_default_library",
    actual = ":go",
    visibility = ["//visibility:public"],
)

bzl_library(
    name = "def",
    srcs = ["def.bzl"],
    visibility = ["//visibility:public"],
    deps = ["@io_bazel_rules_go//go:def"],
)
